
import { FormationManager } from "GameLoop/Execution/FormationManager/FormationManager";
import { FormationPattern } from "GameLoop/Execution/FormationManager/FormationPattern";
import { ATeamEntity, ETeamState } from "..";
import { ATeamCombat } from "./TeamCombat";
import { Team2Pattern } from "GameLoop/Execution/FormationManager/Pattern/team2pattern";
import { PositionHelper } from "utils/position/PositionHelper";
import { Team4Pattern } from "GameLoop/Execution/FormationManager/Pattern/team4pattern";
import { attackRoles } from "utils/constants/bodyconstant";
/**
 * 四人小队
 * 
 */
export class Team4 extends ATeamCombat
{
 
    protected _Formation!: FormationManager<Creep>   ;
    
    public initAttributes(): void 
    {
        super.initAttributes();
        this._Formation = new FormationManager(this,new Team4Pattern<Creep>());

        for(const e of this.getAttacker())
        {
            this._Formation.addCharactor(e);
        }
        for(const e of this.getHealer())
        {
            this._Formation.addCharactor(e);
        }
    }
    public getAttackableCreeps(): Creep[] {
       
        return this.aroundHostileCreeps
    }
    
    public isInCombatRange(pos: IRoomPosition, range: number=1): boolean 
    {
        const lpos = this.getAttacker()[0]?.pos;
        if(lpos)
        {
            return PositionHelper.Distance(lpos,pos)<=range
        }
        return false;
    }
    public isAssembleOver(): boolean {
        return !this.isTeamDismiss()
    }
     /**
     * 投递孵化爬的任务
     */
    public postSpawn(priority:number): void 
    {
        const spawnentity = this.getEntity("SpawnEntity");
        if(this.getNodeMemory().teamState || spawnentity.getTasksByOwner(this.GUID()))
        {
            return 
        }
        const task = this.postSpawnImpl({} as any,priority);
        if(task)
        {
            task.data.teamid = this.GUID();
        }
    }
    public moveByDirection(dir: DirectionConstant): boolean 
    {
        return this.sqareMove(dir);
    }
    private isTeamDismiss()
    {
        const creeps = this.creeps;
        const room_group =_.groupBy(creeps,e=>e.pos.roomName);
        const keys = Object.keys(room_group);
        if(keys.length == 2 )
        {
            if(room_group[keys[0]].length != room_group[keys[1]].length)
            {
                global.Log.Info(`队伍分散了 :${JSON.stringify(Object.keys(room_group))}`);
                return true;
            }
        }
        for(let  i=0;i<creeps.length-1;i++)
        {
            for(let j=i+1;j<creeps.length;j++)
            {
                const pos1 =creeps[i].pos;
                const pos2 =creeps[j].pos;
                if(pos1 && pos2)
                {
                    const distance = PositionHelper.Distance(pos1,pos2);
                   // global.Log.Info(`${JSON.stringify(pos1)}->${JSON.stringify(pos2)}的 距离 :${distance}`);
                    if(pos1.roomName  == pos2.roomName)
                    {
                        if(distance>1)
                        {
                                // 队形已经破坏
                            return true;
                        }
                    }
                    else
                    {
                        if(distance>2)
                        {
                                // 队形已经破坏
                            return true;
                        }
                    }
                }
                else
                {
                    global.Log.Error(`${JSON.stringify(pos1)}->${JSON.stringify(pos2)}的 坐标错误不存在`);
                    // 队形已经破坏
                    return true;
                }
            }
        }
        return false;
    }
}